package com.lc.projects.thread;

public class ThreadPrintABC {
	private static  boolean isPrintA = true;
	private static  boolean isPrintB = false;
	private static  boolean isPrintC = false;

	public static void main(String[] args) {
		final ThreadPrintABC entity = new ThreadPrintABC();
		Thread ta = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i <10; i++) {
					synchronized (entity) {
						while(!isPrintA){
							try {
								System.out.println("wait... a");
								entity.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						System.out.println("A");
						isPrintA = false;
						isPrintB = true;
						isPrintC = false;
						entity.notifyAll();
					}
				}
			}
		});
		
		
		Thread tb = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i <10; i++) {
					synchronized (entity) {
						while(!isPrintB){
							try {
								System.out.println("wait... b");
								entity.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						System.out.println("B");
						isPrintA = false;
						isPrintB = false;
						isPrintC = true;
						entity.notifyAll();
					}
				}
			}
		});
		
		
		Thread tc= new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i <10; i++) {
					synchronized (entity) {
						while(!isPrintC){
							try {
								System.out.println("wait... c");
								entity.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						System.out.println("C");
						isPrintA = true;
						isPrintB = false;
						isPrintC = false;
						entity.notifyAll();
					}
				}
			}
		});
		
		
		tc.start();
		tb.start();
		ta.start();
		
	}
}

/**
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
wait... a
B
wait... b
C
wait... c
wait... b
A
B
C
*/
